package com.haierp.controller.youzan;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.haierp.enums.ItemStatus;
import com.haierp.exception.ErpCommonException;
import com.haierp.model.Item;
import com.haierp.service.IItemService;
import com.haierp.service.item.IOuterItemService;
import com.haierp.service.youzan.IYouzanService;
import com.haierp.util.JsonResult;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.utils.HaiJsonUtils;

/**
 * 
 * 有赞相关的同步接口
 * @author zhulu
 *
 */
@Controller
@RequestMapping("/youzanSyn")
public class YouzanSynController extends BaseController{

	@Autowired
	private IOuterItemService outerItemService;
	
	@Autowired
	private IItemService itemService;
	
	@Autowired
	private IYouzanService youzanService;
	
	/**
	 * 批量同步商品信息到有赞，只能同步新建和上架状态的商品
	 * @param 商品ID
	 * @return
	 */
	@RequestMapping("/batchSynItemYouzan")
	@ResponseBody
	public Object batchSynItemYouzan(String itemIds) {
		JsonResult<String> result = new JsonResult<>();
		StringBuilder sb = new StringBuilder();
		if(StringUtils.isNotEmpty(itemIds)){
			String s = itemIds.replace("&quot;", "\"");
			List<Long> idList = HaiJsonUtils.toBean(s, new TypeReference<List<Long>>(){});
			for(Long id:idList){
				try{
					if(id!=null&&id!=0){
						outerItemService.synItemYouzan(id);
					}
				}catch(ErpCommonException e){
					sb.append(e.getErrorMsg()+",");
				}catch (Exception e) {
					logger.error("同步异常，商品ID：" + id, e);
					sb.append(id +"未知异常"+",");
				}
			}
			
		}else{
			return result.buildIsSuccess(false).buildMsg("没有商品");
		}
		String errorMsg = sb.toString();
		if(StringUtils.isEmpty(errorMsg)){
			return result.buildIsSuccess(true);
		}else{
			return result.buildIsSuccess(false).buildMsg(errorMsg);
		}
	}
	
	
	/**
	 * 批量上架
	 * @param 商品ID
	 * @return
	 */
	@RequestMapping("/batchListingYouzan")
	@ResponseBody
	public Object batchListingYouzan(String itemIds) {
		JsonResult<String> result = new JsonResult<>();
		StringBuilder sb = new StringBuilder();
		if(StringUtils.isNotEmpty(itemIds)){
			String s = itemIds.replace("&quot;", "\"");
			List<Long> idList = HaiJsonUtils.toBean(s, new TypeReference<List<Long>>(){});
			if(CollectionUtils.isNotEmpty(idList)){
				List<Item> items = itemService.selectBatchIds(idList);
				for(Item item:items){
					//if(item.getStatus()==ItemStatus.DELISTING.getCode()){
						try {
							if(item.getCompanyId()==1){
								outerItemService.listingYouzan(item);
							}else{
								throw new ErpCommonException("商品id="+"["+item.getId()+"]不属于如花商品不能执行上架有赞操作");
							}
						}
						 catch (ErpCommonException e) {
								sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+":"+e.getErrorMsg());
							}
						catch (Exception e) {
							e.printStackTrace();
							sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+"未知错误;");
						}
					//}else{
						//sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+"不能上架;");
					//}
				}
			}else{
				return result.buildIsSuccess(false).buildMsg("没有商品");
			}
		}else{
			return result.buildIsSuccess(false).buildMsg("没有商品");
		}
		String errorMsg = sb.toString();
		if(StringUtils.isEmpty(errorMsg)){
			return result.buildIsSuccess(true);
		}else{
			return result.buildIsSuccess(false).buildMsg(errorMsg);
		}
	}
	
	/**
	 * 批量下架
	 * @param 商品ID
	 * @return
	 */
	@RequestMapping("/batchDelistingYouzan")
	@ResponseBody
	public Object batchDelistingYouzan(String itemIds) {
		JsonResult<String> result = new JsonResult<>();
		StringBuilder sb = new StringBuilder();
		if(StringUtils.isNotEmpty(itemIds)){
			String s = itemIds.replace("&quot;", "\"");
			List<Long> idList = HaiJsonUtils.toBean(s, new TypeReference<List<Long>>(){});
			if(CollectionUtils.isNotEmpty(idList)){
				List<Item> items = itemService.selectBatchIds(idList);
				for(Item item:items){
					//if(item.getStatus()==ItemStatus.LISTING.getCode()){
						try {
							if(item.getCompanyId()==1){//如花商品同步到有赞
								outerItemService.delistingYouzan(item);
							}else{
								throw new ErpCommonException("商品id="+"["+item.getId()+"]不属于如花商品不能执行到有赞");
							}
						}
						 catch (ErpCommonException e) {
								sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+":"+e.getErrorMsg());
							}
						catch (Exception e) {
							e.printStackTrace();
							sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+"未知错误;");
						}
					//}else{
						//sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+"不能下架;");
					//}
				}
			}else{
				return result.buildIsSuccess(false).buildMsg("没有商品");
			}
		}else{
			return result.buildIsSuccess(false).buildMsg("没有商品");
		}
		String errorMsg = sb.toString();
		if(StringUtils.isEmpty(errorMsg)){
			return result.buildIsSuccess(true);
		}else{
			return result.buildIsSuccess(false).buildMsg(errorMsg);
		}
	}
	
	@RequestMapping("/test")
	@ResponseBody
	public Object test() {
		JsonResult<String> result = new JsonResult<>();
		youzanService.youzanItemcategoriesGet();
		System.out.println("=========");
		youzanService.youzanItemcategoriesTagsGet();
				
		return result;
	}
}
